<?php

namespace App\Admin\Forms;

use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use App\Models\ShopCom;
use App\Models\Commodity;

class Addshopcom extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '添加店铺商品';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {
        // dump($request->all());
        $is_has = ShopCom::where(['shop_id'=>$request->shop_id,'commodity_attr_id'=>$request->commodity_attr_id,'is_buy'=>$request->is_buy])->first();
        if($is_has){
            admin_error('添加失败.','店铺已有该商品');
            return back();
        }
        $shopcom = new ShopCom();
        $shopcom->fill($request->all());

        $shopcom->brand_id = Commodity::where('id',$request->commodity_id)->value('brand_id');
        $shopcom->save();
        admin_success('添加成功.');

        return back();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        $this->select('shop_id', __('店铺名称'))->options('/admin/shop/shopList')->required();
        // $this->select('brand_id', __('品牌'))->options('/admin/brands/get_brand_list')->required();
        $this->select('commodity_id', __('商品名称'))->options('/admin/commodities/comList')->load('commodity_attr_id', '/admin/commodities/attrList')->required();
        $this->select('commodity_attr_id', __('商品属性'))->required();
        $this->number('stock', __('库存'))->default(0);
        $this->decimal('price', __('换购价格'))->default(0.00);
        $this->radio('is_buy', __('能否换购'))->options([1 => '是', 0 => '否'])->default(1);
        $this->radio('status', __('是否上架'))->options([1 => '是', 2 => '否'])->default(1);
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        return [
            'name'       => 'John Doe',
            'email'      => 'John.Doe@gmail.com',
            'created_at' => now(),
        ];
    }
}
